let Transform = require('./transform');
let {vec3, mat4} = require('gl-matrix');

//
class GLObj {

    /**
     *
     * @param data =  {
        positions: posArray,
        normals: normalArray,
        texCoords: texCoordArray,
        indices: indicesArray,
    };
     **/
    constructor(shaderInfo,data) {
        let gl = globalThis.app.gl;
        this.shaderInfo = shaderInfo;

        // vao
        let vao = this.vao = gl.createVertexArrayOES();
        gl.bindVertexArrayOES(vao);

        // posVBO
        let posVBO = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, posVBO);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(data.positions),gl.STATIC_DRAW);
        gl.enableVertexAttribArray(shaderInfo.attribs['a_position']);
        gl.vertexAttribPointer(shaderInfo.attribs['a_position'],3,gl.FLOAT,false,0,0);

        // normalVBO
        let normalVBO = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER,normalVBO);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(data.normals),gl.STATIC_DRAW);
        gl.enableVertexAttribArray(shaderInfo.attribs['a_normal']);
        gl.vertexAttribPointer(shaderInfo.attribs['a_normal'],3,gl.FLOAT,false,0,0);

        // indiceVBO
        let indiceVBO = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indiceVBO);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(data.indices),gl.STATIC_DRAW);
        this.drawVertexCount = data.indices.length;
        gl.bindVertexArrayOES(null);
    }
}


class Obj {
    constructor(shader,data) {
        this.transform = new Transform();
        this.color = vec3.fromValues(1, 1, 1); // 相当于材质
        this.globj = new GLObj(shader,data);
    }

    update(dt){

    }

    depthPass(){}
}

module.exports = Obj;
